// custom project config

const { name } = require('./package')
const path = require('path')
const proxyConfig = require('./proxyConfig.js')
const fs = require('fs')
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')
const port = 8502

const globalSassVars = fs.readFileSync('src/styles/_variables.scss', {
  encoding: 'utf8'
})
const globalSassMixins = fs.readFileSync('src/styles/_mixins.scss', {
  encoding: 'utf8'
})
module.exports = {
  productionSourceMap: false,
  publicPath: process.env.BASE_URL,
  // ! be care of follow config, do not change the related config in configureWebpack
  // publicPath, outputDir, indexPath
  chainWebpack: config => {
    /**
     * noparse.local.js 用于排除当前不需要开发的模块，进一步提高编译速度
     * 该文件不会提交到远程仓库
     * 可以查看webpack文档
     * https://www.webpackjs.com/configuration/module/#module-noparse
     */
    if (fs.existsSync('./noparse.local.js')) {
      config.module.noParse(require('./noparse.local'))
    }
    config.output.set('pathinfo', false)

    /**
     * 通过以下插件得知：耗时的并非ts或vue，而是style-resource-loader
     * 因此去掉该loader，改用sass-loader的data选项来支持scss全局变量
     *
     * 热更新速度保持在1s以内(Leneovo T470)
     */
    process.env.NODE_ENV === 'development' &&
      config
        .plugin('sec')
        .use(new SpeedMeasurePlugin())
        .end()
    return config
  },
  configureWebpack: {
    resolve: {
      alias: {
        vue$: 'vue/dist/vue.esm.js',
        '@': path.join(__dirname, './src'),
        '@config': path.join(__dirname, './src/config')
      },
      extensions: ['.js', '.vue', '.json', '.css']
    },
    output: {
      // 把子应用打包成 umd 库格式
      library: `${name}`,
      libraryTarget: 'umd',
      jsonpFunction: `webpackJsonp_${name}`
    }
  },
  pluginOptions: {
    // 去掉该loader
    'style-resources-loader': {
      preProcessor: 'scss',
      patterns: []
    },

    dll: {
      entry: {
        vue: ['vue', 'vue-router', 'vuex'],
        vue_att: ['vue-video-player', 'vue-seamless-scroll', 'vue-codemirror'],
        emui: ['element-ui'],
        echarts: ['echarts', 'echarts-gl'],
        axios: ['axios'],
        utils: ['core-js', 'lodash', 'sql-formatter', 'screenfull', 'driver.js']
      }
    }
  },
  css: {
    loaderOptions: {
      sass: {
        // 注入到文件中
        data: `
        ${globalSassVars}
        ${globalSassMixins}
        `
      }
    }
  },
  pwa: {
    iconPaths: {
      favicon32: './favicon.ico',
      favicon16: './favicon.ico',
      appleTouchIcon: './favicon.ico',
      maskIcon: './favicon.ico',
      msTileImage: './favicon.ico'
    }
  },
  devServer: {
    overlay: {
      warnings: true,
      errors: true
    },
    open: true,
    port: port,
    headers: {
      'Access-Control-Allow-Origin': '*'
    },
    proxy: Object.assign(
      {
        // portal 系统授权菜单树等
        [process.env.VUE_APP_BASE_API]: {
          // target: 'http://127.0.0.1:8500/portal/std', // 目标代理接口地址
          // target: 'http://portal.poc.tengsaw.com/api/portal/std', // poc
          // target: 'http://portal.st.tengsaw.com/api/portal/std', // 标准版
          // target: 'http://172.16.0.42:8088/api/portal/std',
          // target: 'http://api.portal.com/api/portal',
          target: 'http://172.16.0.45:8181/std/',
          secure: false,
          changeOrigin: true, // 开启代理，在本地创建一个虚拟服务端
          // ws: true, // 是否启用websockets
          pathRewrite: {
            ['^' + process.env.VUE_APP_BASE_API]: ''
          }
        }
      },
      proxyConfig
    )
  }
}
